import {jsPDF} from 'jspdf'
import html2canvas from 'html2canvas'
// import Canvg from 'canvg'
// import * as rasterizeHTML from 'rasterizehtml'

const toPdf = (dom) => {
  // let svgElem = dom.querySelectorAll('svg')
  // for (let i = 0; i < 0; i++) {
  //   let node = svgElem[i]
  //   let parentNode = node.parentNode
  //   let svg = node.outerHTML.trim()
  //   let canvas = document.createElement('canvas'),
  //     ctx = canvas.getContext('2d')
  //   const v = await Canvg.from(ctx, svg)
  //
  //   // Render only first frame, ignoring animations and mouse.
  //   await v.render()
  //   canvas.style.zIndex = 9
  //   if (node.style.position) {
  //     canvas.style.position += node.style.position
  //     canvas.style.left += node.style.left
  //     canvas.style.top += node.style.top
  //   }
  //   parentNode.removeChild(node)
  //   parentNode.appendChild(canvas)
  // }
  html2canvas(dom,
    {
      allowTaint: true,
      backgroundColor: 'white',
      useCORS: true // 允许CORS跨域
    }).then(canvas => {
    let url = canvas.toDataURL('image/png', 1)
    // eslint-disable-next-line new-cap
    let pdf = new jsPDF('', 'px', 'a4')
    pdf.addImage(url, 'png', 0, 0, 410, 580)
    pdf.save('test.pdf')
  })
  // let canvas = document.createElement('canvas')
  // rasterizeHTML.draw(dom, canvas).then((res) => {
  //   // eslint-disable-next-line new-cap
  //   let pdf = new jsPDF('', 'px', 'a4')
  //   console.log(res.image)
  //   pdf.addImage(res.image, 'png', 0, 0, 410, 580)
  //   pdf.save('test.pdf')
  // })
}

export default toPdf
